<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/
// created :	18 March 2010
// version :	18 March 2010

class Cmis_Repository_Capabilities
{
	/**
	 * @var array[Cmis_Repository_Capability_Interface]
	 */
	protected $_capabilities;

	public function __construct(array $capabilities = null)
	{
		$this->_capabilities = array();
		if (!empty($capabilities))
		{
			array_walk($capabilities, array($this, 'mkAddCapability'));
		}
	}

	/**
	 * Create and add the capability
	 * @param string $name
	 * @param string $value
	 * @return Cmis_Repository_Capability_Interface|null nul if no exist
	 */
	public function mkAddCapability($value, $name)
	{
		//skip capability in name
		$name = str_replace('capability', '', $name);
		$capability = 'Cmis_Repository_Capability_' . ucfirst($name);
		$capa = null;
		if (class_exists($capability, true))
		{
			$capa = new $capability($name, $value);
			$this->add($capa); 
		}
		return $capa;
	}

	/**
	 * Set or replace same type (key) of capability 
	 * @param Cmis_Repository_Capability_Interface $capability
	 * @return Cmis_Repository_Capabilities
	 */
	public function add(Cmis_Repository_Capability_Interface $capability)
	{
		$this->_capabilities[ $capability->getKey() ] = $capability;

		return $this;
	}

	/**
	 * Get the capability
	 * @param string $key
	 * @return Cmis_Repository_Capability_Interface
	 */
	public function get($key)
	{
		if (!isset($this->_capabilities[ $key ]))
		{
			var_dump(array_keys($this->_capabilities), $this->_capabilities);
            throw new Cmis_Exception_Repository_Capability_DontExist($key);
		}
		return $this->_capabilities[ $key ];
	}

	/**
     * Get the capability's value
     * @param string $key
     * @return mixed
	 */
	public function value($key)
	{
		 return $this->get($key)->getValue();
	}
}

# EOF
